// packageB/pages/package/liangdeng/bind_positon/bind_positon.js
const mybehavior = require("../../../../../behaviors/requests")
import WxValidate from "../../../../../Wx_Validate/WxValidate"
Page({

  /**
   * 页面的初始数据
   */
  behaviors: [mybehavior],
  data: {
    baseUrl: "https://logisticsengineering.gree.com/order_system",
    company: "",
    manager: "",
    line: "",
    position: "",
    position_list: [],
    position_list1: [],
    label_mac: "",
    label_list: [],
    label_list1: [],
    indicator_mac: "",
    indicator_list: [],
    options: [],
    options1: [],
    options11: [],
    search_value: "",
    options22: [],
    search_value2: "",
    options3: [],
    popup_show1: false,
    popup_show2: false,
    popup_show3: false,
    radio: "1",
    radio_show: true,
    many_test_show: true,
  },
  onRadioChange(event) {
    if (event.detail == "1") {
      this.setData({
        radio: event.detail,
        many_test_show: true,
      });
    } else {
      this.setData({
        radio: event.detail,
        many_test_show: false,
      });
    }

  },
  onSearch1(e) {
    console.log(e.detail);

    if (e.detail == "") {
      let arr = this.data.options11
      this.setData({
        options1: arr
      })
    } else {
      let list1 = this.data.options11
      let arr = []
      for (let i = 0; i < list1.length; i++) {
        if (list1[i].match(this.data.search_value) != null) {
          arr.push(list1[i]);
        }
      }
      this.setData({
        options1: arr
      })
    }
  },
  onSearch2(e) {
    console.log(e.detail);

    if (e.detail == "") {
      let arr = this.data.options22
      this.setData({
        options2: arr
      })
    } else {
      let list1 = this.data.options22
      let arr = []
      for (let i = 0; i < list1.length; i++) {
        if (list1[i].match(this.data.search_value2) != null) {
          arr.push(list1[i]);
        }
      }
      this.setData({
        options2: arr
      })
    }
  },
  // 点击--公司名称--选取输入框
  onClick1(event) {
    // event.detail 为当前输入的值
    // console.log(event.detail);
    this.setData({
      popup_show1: true
    })
  },
  // 选择--公司名称--输入框关闭
  onClose1() {
    this.setData({ popup_show1: false });
  },
  // 确认--公司名称值--弹出框关闭
  onChange1(event) {
    const { picker, value, index } = event.detail;
    const arr = this.data.options[value]
    this.setData({
      company: value,
      options2: arr,
      options22: arr,
      popup_show1: false,
      manager: "",
      line: "",
    })
  },


  // 点击--单位--选取输入框
  onClick2(event) {
    // event.detail 为当前输入的值
    // console.log(event.detail);
    this.setData({
      popup_show2: true
    })
  },
  // 选择--单位--输入框关闭
  onClose2() {
    this.setData({ popup_show2: false });
  },
  // 确认--单位--弹出框关闭
  onChange2(event) {
    const { picker, value, index } = event.detail;
    this.setData({
      manager: value,
      line: "",
      popup_show2: false,
    })
    this.getLineDatas()
  },



  // 获取--公司名称--数据
  getCompanyDatas() {
    let url = this.data.baseUrl + "/get_u_permission"
    let data = {

    }
    this.post(url, data).then(res => {
      // console.log("公司名称", res.data)
      let data_list = Object.keys(res.data)

      if (res.code == 20000) {

        this.setData({
          options1: data_list,
          options11: data_list,
          options: res.data,

        })

      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },


  // 点击--仓库选取--选取输入框
  onClick3(event) {
    // event.detail 为当前输入的值
    // console.log(event.detail);
    this.setData({
      popup_show3: true
    })
  },
  // 选择--仓库选取--输入框关闭
  onClose3() {
    this.setData({ popup_show3: false });
  },
  // 确认--仓库选取--弹出框关闭
  onChange3(event) {
    const { picker, value, index } = event.detail;
    this.setData({
      line: value,
      popup_show3: false,
    })
    // this.seeManyCheckRes()
  },
  // 批量查询检测结果
  seeManyCheckRes() {
    wx.showLoading({
      title: '数据加载中',
    })
    let url = this.data.baseUrl + "/inspect_status_by_batch"
    let data = {
      company: this.data.company,
      manager: this.data.manager,
      line: this.data.line,
    }
    console.log(data);


    this.post(url, data).then(res => {

      if (res.code == 20000) {
        wx.hideLoading()
        wx.showModal({
          title: '',
          content: '已开始进行检测,确认是否前往查询检测结果?',
          confirmText: '立即查询',
          cancelText: '稍后查询',
          complete: (res) => {
            if (res.cancel) {

            }
            if (res.confirm) {
              wx.navigateTo({
                url: '/packageB/pages/package/liangdeng/test_label/check_res/check_res'
              })
            }
          }
        })


      } else {
        wx.hideLoading()
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      wx.hideLoading()
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },


  // 获取--仓库选取--数据
  getLineDatas() {
    let url = this.data.baseUrl + "/positions_lines"
    let data = {
      company: this.data.company,
      manager: this.data.manager,
    }
    this.post(url, data).then(res => {
      console.log("仓库选取", res.data)

      if (res.code == 20000) {

        this.setData({
          options3: res.data,

        })

      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },
  // 扫描
  scan1(e) {
    console.log(e.currentTarget.dataset.row_data);
    let key = e.currentTarget.dataset.row_data
    const self = this;
    wx.scanCode().then((res) => {
      const label_code = res.result;
      wx.showLoading({
        title: "loading"
      })
      console.log(label_code);
      setTimeout(function () {
        wx.hideLoading()
        self.setData({
          [key]: label_code
        })
        self.addLabel()
      }, 500)
    }).catch(err => {
      console.log(err);
    });
  },
  // 添加标签
  addLabel() {
    if (this.data.label_mac == "") {
      wx.showToast({
        title: '标签号为空，无法添加!',
        icon: 'none'
      })

    } else {

      if (this.data.label_list1.includes(this.data.label_mac)) {
        wx.showToast({
          title: '请勿重复添加!',
          icon: 'none'
        })

      } else {
        let arr = this.data.label_list
        let arr1 = this.data.label_list1
        console.log(arr.length);
        let obj = {
          index: arr.length + 1,
          label_mac: this.data.label_mac
        }
        arr.push(obj)
        arr1.push(this.data.label_mac)
        this.setData({
          label_list: arr,
          label_list1: arr1,
          label_mac: ""
        })
      }
    }
  },
  // 删除标签
  deleteLabel(e) {
    console.log(e.currentTarget.dataset.row_data);
    let key = e.currentTarget.dataset.row_data - 1
    let arr = this.data.label_list
    let arr1 = this.data.label_list1
    arr.splice(key, 1)
    for (let i in arr) {
      arr[i].index = Number(i) + Number(1)
    }
    arr1.splice(key, 1)
    this.setData({
      label_list: arr,
      label_list1: arr1,
    })
  },



  // 提交绑定
  submit(e) {
    let that = this
    let url = this.data.baseUrl + "/inspect__status_by_code"
    let data = {
      codes: this.data.label_list1,
    }
    console.log(data);


    this.post(url, data).then(res => {

      if (res.code == 20000) {
        wx.showModal({
          title: '',
          content: '已开始进行检测,确认是否前往查询检测结果?',
          confirmText: '立即查询',
          cancelText: '稍后查询',
          complete: (res) => {
            if (res.cancel) {

            }
            if (res.confirm) {
              wx.navigateTo({
                url: '/packageB/pages/package/liangdeng/test_label/check_res/check_res',
              })
            }
          }
        })

      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })


  },
  seeCheckRes() {
    if (this.data.radio == "1") {
      wx.navigateTo({
        url: '/packageB/pages/package/liangdeng/test_label/check_res/check_res?flag=solo',
      })
    } else {
      let data = {
        company: this.data.company,
        manager: this.data.manager,
        line: this.data.line,
      }
      let that = this
      if (!that.WxValidate.checkForm(data)) {
        const error = that.WxValidate.errorList[0]
        wx.showToast({
          title: error.msg,
          icon: 'none'
        })
      } else {
        this.seeManyCheckRes()


      }

    }



  },
  // 成功后删除库位
  // 删除库位
  deletePosition1(e) {
    let key = e
    let arr = this.data.position_list
    let arr1 = this.data.position_list1
    for (let i in arr) {
      if (arr[i].position == key) {
        arr.splice(i, 1)

      }
      if (arr.length != 0) {
        arr[i].index = Number(i) + Number(1)

      }
    }
    for (let j in arr1) {
      if (arr1[j].position == key) {
        arr1.splice(i, 1)

      }

    }
    this.setData({
      position_list: arr,
      position_list1: arr1,
    })
  },
  //初始化表单验证规则
  initValidate() {
    let rules = {
      company: {
        required: true,
      },
      manager: {
        required: true,
      },
      line: {
        required: true,
      },

    }

    let message = {
      company: {
        required: "公司名称-不能为空",
      },
      manager: {
        required: "单位-不能为空",
      },
      line: {
        required: "仓库选取-不能为空",
      },
    }
    //实例化当前的验证规则和提示消息
    this.WxValidate = new WxValidate(rules, message);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.initValidate()

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getCompanyDatas()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})